/********************************************************************
 * PROJECT: CSAIR ROST PROJECT.
 * SUPPLIER: FOREVER OPEN SOURCE SOFTWARE INC.
 ********************************************************************
 * FILE: GanttGridJointHandler.java
 * (C) Copyright Foliage Team 2013, All Rights Reserved.
 ********************************************************************/
package org.foliage.rcp.ui.handlers;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Widget;
import org.foliage.rcp.ui.custom.GanttGrid;
import org.foliage.rcp.ui.custom.GanttGridColumn;
import org.foliage.rcp.ui.custom.GanttGridItem;
import org.foliage.rcp.ui.preference.IGanttSettings;
import org.foliage.rcp.ui.widgets.Grid;

@SuppressWarnings({ "rawtypes", "unchecked" })
public class GanttGridJointHandler implements IGanttGridJointHandler {

	// ----------------------------------------------- 

	private Set members = new HashSet();

	// ----------------------------------------------- 

	public GanttGridJointHandler(GanttGrid[] widgets) {
		members.addAll(Arrays.asList(widgets));
	}

	// ----------------------------------------------- 

	@Override
	public void jointHorizontalMovement(int diff) {
		for (Iterator iter = members.iterator(); iter.hasNext();) {
			GanttGrid ganttGrid = (GanttGrid) iter.next();
			ganttGrid.getGantt().moveHorizontal(diff);
		}
	}

	@Override
	public void jointGanttRedrawing() {
		for (Iterator iter = members.iterator(); iter.hasNext();) {
			GanttGrid ganttGrid = (GanttGrid) iter.next();
			ganttGrid.getGantt().redraw();
		}
	}

	@Override
	public void jointSashMovement(Event event) {
		for (Iterator iter = members.iterator(); iter.hasNext();) {
			GanttGrid ganttGrid = (GanttGrid) iter.next();
			FormData data = (FormData) ganttGrid.getSash().getLayoutData();
			data.left.offset = event.x;
			ganttGrid.layout();
		}
	}

	@Override
	public void jointGanttSettings(IGanttSettings settings) {
		for (Iterator iter = members.iterator(); iter.hasNext();) {
			GanttGrid ganttGrid = (GanttGrid) iter.next();
			ganttGrid.getGantt().setGanttSettings(settings);
		}
	}

	@Override
	public void jointColumnResize(Event event) {
		GanttGridColumn column = (GanttGridColumn) event.widget.getData(GanttGridColumn.DATA_KEY);
		int index = column.getIndex();
		int width = column.getWidth();
		for (Iterator iter = members.iterator(); iter.hasNext();) {
			GanttGrid ganttGrid = (GanttGrid) iter.next();
			ganttGrid.getColumn(index).setWidth(width);
		}
	}

	@Override
	public void jointColumnSorting(int index, int sortStyle) {
		for (Iterator iter = members.iterator(); iter.hasNext();) {
			GanttGrid ganttGrid = (GanttGrid) iter.next();
			ganttGrid.getColumn(index).setSort(sortStyle);
		}
	}

	@Override
	public GanttGridItem[] jointGettingItems() {
		List items = new ArrayList();
		for (Iterator iter = members.iterator(); iter.hasNext();) {
			GanttGrid ganttGrid = (GanttGrid) iter.next();
			items.addAll(Arrays.asList(ganttGrid.getItems()));
		}
		return (GanttGridItem[]) items.toArray(new GanttGridItem[items.size()]);
	}

	@Override
	public void jointGridRedrawing() {
		for (Iterator iter = members.iterator(); iter.hasNext();) {
			GanttGrid ganttGrid = (GanttGrid) iter.next();
			ganttGrid.getGrid().redraw();
		}
	}

	@Override
	public void jointColumMoving(Event event) {
		GanttGridColumn column = (GanttGridColumn) event.widget.getData(GanttGridColumn.DATA_KEY);
		Grid grid = column.getParent().getGrid();
		for (Iterator iter = members.iterator(); iter.hasNext();) {
			GanttGrid ganttGrid = (GanttGrid) iter.next();
			ganttGrid.getGrid().setColumnOrder(grid.getColumnOrder());
			ganttGrid.getGrid().redraw();
		}
	}

	// ----------------------------------------------- 

	@Override
	public void addMember(Widget member) {
		if (!(member instanceof GanttGrid)) {
			SWT.error(SWT.ERROR_INVALID_ARGUMENT);
		}
		members.add(member);
	}

}
